package com.lqs.sofp.service.impl;

import com.lqs.sofp.dao.UserInfoDao;
import com.lqs.sofp.dao.impl.UserInfoDaoImpl;
import com.lqs.sofp.entity.User;
import com.lqs.sofp.service.UserService;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author lkh&cj
 */
public class UserServiceImpl implements UserService {
    @Override
    public ArrayList<User> getAllUser() {
        UserInfoDao userInfo = new UserInfoDaoImpl();
        ArrayList<User> userList = userInfo.selectAll();
        return userList;
    }

    @Override
    public User login(String username, String password) {
        UserInfoDao userinfo = new UserInfoDaoImpl();
        User user = userinfo.select(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public Boolean register(String username, String password, String phone, String gender) {
        User user = new User(username, password, phone, gender);

        //对不符合规则的信息筛选
        String usernameRegular = "\\w{2,12}";
        String passwordRegular = "\\w{3,12}";
        String phoneRegular = "1\\d{10}";

        Matcher userMatcher = Pattern.compile(usernameRegular).matcher(username);
        Matcher passMatcher = Pattern.compile(passwordRegular).matcher(phone);
        Matcher phoneMatcher = Pattern.compile(phoneRegular).matcher(phone);
        //都符合正则表达式的内容
        if (userMatcher.matches() && passMatcher.matches() && phoneMatcher.matches()) {
            UserInfoDao userInfo = new UserInfoDaoImpl();
            Boolean success = userInfo.update(user);
            return success;
        } else {
            return false;
        }


    }
}
